package com.xgkj.diyiketang.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.data.SellerInfo;
import com.xgkj.diyiketang.http.CouponHttpRequest;
import com.xgkj.diyiketang.http.CouponHttpResponse;
import com.xgkj.diyiketang.http.engine.CouponHttpBaseTask;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrater on 2015/12/8.
 */
public class BaoHanShangJiaTask extends CouponHttpBaseTask {
    private int mSize;
    private int mPageNo;
    private String mTradingId;
    private String mAreaName;
    private static final String SEARCHVIDEOJSON = "businessList";


    public BaoHanShangJiaTask(int size, int pageNo,String tradingAreaId,String areaName) {
        super(TaskType.BAOHANSHANGJIALIST_TASK);
        mSize = size;
        mPageNo = pageNo;
        mTradingId = tradingAreaId;
        mAreaName = areaName;
    }

    @Override
    protected void processResponseData(CouponHttpResponse response) {
        JSONObject respJson = response.getBody();
        int pageCount = 0;
        int currentPage = 0;
        int nextPage = 0;
        List<SellerInfo> list = new ArrayList<SellerInfo>();
        if (respJson != null) {
            JSONObject jsonString = respJson.getJSONObject(SEARCHVIDEOJSON);
            currentPage = jsonString.getIntValue(ProtocolConst.CURRENT_PAGE);
            pageCount = jsonString.getIntValue(ProtocolConst.PAGE_COUNT);
            nextPage = currentPage >= pageCount ? 0 : (currentPage + 1);
            list = JSON.parseArray(jsonString.getString(ProtocolConst.LIST_TAG), SellerInfo.class);
        }

        notifyTaskCompleted(nextPage, list);

    }

    @Override
    protected void prepareRequest() {
        super.prepareRequest();
        if (mPhase == TASK_PHASE_INIT) {
            CouponHttpRequest request = new CouponHttpRequest(RequestType.BAOHANSHANGJIALIST_REQUEST);
            JSONObject json = new JSONObject();
            json.put("size", mSize);
            json.put("pageNo", mPageNo);
            json.put("tradingAreaId", mTradingId);
            json.put("areaName",mAreaName);
            request.setData(json.toString());
            request.setApiNo(ApiNo.BAOHANSHANGJIALIST);
            addRequest(request);
        }
    }
}
